<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑</title>
<meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../../res/css/wholesale/font.css">
  	<link rel="stylesheet" href="../../res/css/wholesale/xadmin.css">
  	<link rel="stylesheet" href="../../res/layui/css/layui.css">
  	<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
 	<script type="text/javascript" src="../../res/layui/layui.js" charset="utf-8"></script>
  	<script type="text/javascript" src="../../res/js/wholesale/xadmin.js"></script>
</head>
<body>
	<form id="form" class="layui-form" action="../../listenUserRole/update.action" method="post" style="margin-top: 10px;">
		<input type="hidden" id="roleId" name="roleId">
		<div class="layui-form-item" style="width:70%;margin-left:30px">
              <label for="username" class="layui-form-label">用户姓名：</label>
              <div class="layui-input-block">
                  <select name="buId" id="buId" class="valid" lay-search lay-verify="required">
                  </select>
              </div>
         </div>
         
         <div class="layui-form-item" style="width:70%;margin-left:30px">
			<label class="layui-form-label">角色：</label>
			<div class="layui-input-block" id="roles">
		    </div>
		 </div>
	  	
	 <br/>
	  <div class="layui-form-item" style="width:70%;margin-left:35px">
	    <div class="layui-input-block">
	      <button class="layui-btn" lay-submit lay-filter="formDemo" id="sub">立即提交</button>
	    </div>
	   </div>
	</form>
<script>
$(function(){
	jQuery.ajaxSettings.async = false;
})
layui.use(['form', 'layedit', 'laydate'], function(){
  var form = layui.form
  ,laydate = layui.laydate
  ,layer = layui.layer
  ,layedit = layui.layedit;
  
	//日期
  laydate.render({
	  elem:'#date'
   });
  
	
  form.on('checkbox(allChoose)', function(obj){
		if(obj.elem.checked == true) {
			var str = "";
			str+=obj.value+"@";
			$("#roleId").val($("#roleId").val()+str);
		} else {
			 //取得文本框的值
			 var aa = $("#roleId").val();
			 //把文本框清空
			 $("#roleId").val("")
			 //文本框的值按取消复选框的id来去除
			 var bb = aa.replace(obj.value+"@",'');
			 //把值重新赋给文本框
			 $("#roleId").val(bb);
		}
	});

	
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');
 
  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 1||value.length>7){
        return '长度至少得1个字符并且不能大于7个字符';
      }
    }
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });
  var url="../../listenBackstageUser/queryAll.action";
  var data=null;
  $.post(url,data,function(mes){
	  $.each(mes,function(index,item){
		  $("#buId").append("<option></option><option name='bu' value="+item.buId+">"+item.buName+"</option>");
		  form.render();
	  });
  },"json");
  
  var url="../../listenRole/queryAll.action";
  var data=null;
  $.post(url,data,function(mes){
	  $.each(mes,function(index,item){
		  $("#roles").append("<input type='checkbox' name='role' value="+item.roleId+" title="+item.roleName+" lay-filter='allChoose'> ");
		  form.render();
	  });
  },"json");
	
  //监听提交
  form.on('submit(formDemo)', function(data){
		  window.parent.location.reload()
		  var index=parent.layer.getFrameIndex(window.name);
		  parent.layer.close(index);
  });
 
 
  
});

function load(){
	var form = layui.form;
	form.render();
}
</script>
</body>
</html>